package com.cskaoyan.service;

import com.cskaoyan.bean.BaseData;
import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.goods.*;
import com.cskaoyan.bean.goods.catbrand.BrandList;
import com.cskaoyan.bean.goods.catbrand.CatAndBrand;
import com.cskaoyan.bean.goods.catbrand.CategoryList;
import com.cskaoyan.mapper.goodsMapper.MarketGoodsAttributeMapper;
import com.cskaoyan.mapper.goodsMapper.MarketGoodsMapper;
import com.cskaoyan.mapper.goodsMapper.MarketGoodsProductMapper;
import com.cskaoyan.mapper.goodsMapper.MarketGoodsSpecificationMapper;
import com.cskaoyan.mapper.goodsMapper.catbrand.MarketBrandMapper;
import com.cskaoyan.mapper.goodsMapper.catbrand.MarketCategoryMapper;
import com.cskaoyan.util.PageUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author Hu
 * @create 2021-09-16 19:48
 * @Description
 */

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    MarketGoodsMapper goodsMapper;

    @Autowired
    MarketGoodsSpecificationMapper goodsSpecificationMapper;

    @Autowired
    MarketGoodsProductMapper goodsProductMapper;

    @Autowired
    MarketGoodsAttributeMapper goodsAttributeMapper;


    @Override
    public BaseData<MarketGoods> list(BaseParam baseParam, Integer goodsId, String goodsSn, String name) {

        if (baseParam.getLimit()==0&&(goodsId==null)&&(goodsSn==null)&&(name==null)){
            List<MarketGoods> goods = goodsMapper.selectByExample( new MarketGoodsExample());
            PageInfo<MarketGoods> pageInfo = new PageInfo<>(goods);
            BaseData<MarketGoods> data = new BaseData<>(pageInfo, goods);
            return data;

        }

        PageUtil.start(baseParam);
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        marketGoodsExample.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (goodsId != null && !"".equals(goodsId)) {
            criteria.andIdEqualTo(goodsId);
        }
        if (goodsSn != null && !"".equals(goodsSn)) {
            criteria.andGoodsSnEqualTo(goodsSn);
        }

        List<MarketGoods> goods = goodsMapper.selectByExample(marketGoodsExample);
        PageInfo<MarketGoods> pageInfo = new PageInfo<>(goods);
        BaseData<MarketGoods> data = new BaseData<>(pageInfo, goods);


        return data;
    }

    @Override
    public void create(Goods4Class goodsInfo) {
        //对SnId进行判空
        if (goodsInfo.getGoods().getGoodsSn() == null && "".equals(goodsInfo.getGoods().getGoodsSn())) {
            //todo:此处应该抛异常 并且整合tx
            return;
        }
        //para1插入操作  new Data()是当前值么?
        //传出主键id!!! 给后面三个小类使用
        goodsInfo.getGoods().setAddTime(new Date(System.currentTimeMillis()));
        goodsInfo.getGoods().setDeleted(false);
        BigDecimal retailPrice = new BigDecimal(9999999);
        for (MarketGoodsProduct tempProduct : goodsInfo.getProducts()) {
            if (tempProduct.getPrice().compareTo(retailPrice) == -1) {
                retailPrice = tempProduct.getPrice();
            }
        }

        goodsInfo.getGoods().setRetailPrice(goodsInfo.getGoods().getCounterPrice());
        //TODO:retailprice需要优化

        goodsMapper.insert(goodsInfo.getGoods());
        int id = goodsInfo.getGoods().getId();


        //para2插入操作
        for (MarketGoodsSpecification tempSpec : goodsInfo.getSpecifications()) {
            tempSpec.setAddTime(new Date(System.currentTimeMillis()));
            tempSpec.setGoodsId(id);
            tempSpec.setDeleted(false);
            goodsSpecificationMapper.insert(tempSpec);
        }

        //para3插入操作
        for (MarketGoodsProduct tempProduct : goodsInfo.getProducts()) {
            tempProduct.setAddTime(new Date(System.currentTimeMillis()));
            tempProduct.setGoodsId(id);
            tempProduct.setDeleted(false);
            goodsProductMapper.insert(tempProduct);
        }


        //para4插入操作
        for (MarketGoodsAttribute tempAttribute : goodsInfo.getAttributes()) {
            tempAttribute.setAddTime(new Date(System.currentTimeMillis()));
            tempAttribute.setGoodsId(id);
            tempAttribute.setDeleted(false);
            goodsAttributeMapper.insert(tempAttribute);
        }

    }


    @Override
    public Goods4Class detail(Integer goodsid) {

        MarketGoods marketGoods = goodsMapper.selectByPrimaryKey(goodsid);

        List<MarketGoodsSpecification> specification = goodsSpecificationMapper.selectSpecByGoodsId(goodsid);

        List<MarketGoodsProduct> marketGoodsProduct = goodsProductMapper.selectProductByGoodsId(goodsid);

        List<MarketGoodsAttribute> marketGoodsAttribute = goodsAttributeMapper.selectAttributeByGoodsId(goodsid);

        Goods4Class goods4Class = new Goods4Class(marketGoods, specification, marketGoodsProduct, marketGoodsAttribute);
        return goods4Class;
    }

    @Override
    public void delete(Integer goodsid) {
        goodsMapper.deleteByPrimaryKeyInLogic(goodsid);

        goodsSpecificationMapper.deleteSpecByGoodsIdInLogic(goodsid);

        goodsProductMapper.deleteProductByGoodsIdInLogic(goodsid);

        goodsAttributeMapper.deleteAtrByGoodsIdInLogic(goodsid);
    }

    @Override
    public void update(Goods4Class goodsInfo) {
        Integer goodsid = goodsInfo.getGoods().getId();
        //用主键id更新Goods 然后对子表进行逻辑删除后添加子表
        goodsMapper.updateByPrimaryKey(goodsInfo.getGoods());


        //子表删除
        goodsSpecificationMapper.deleteSpecByGoodsIdInLogic(goodsid);
        goodsProductMapper.deleteProductByGoodsIdInLogic(goodsid);
        goodsAttributeMapper.deleteAtrByGoodsIdInLogic(goodsid);

        //子表新增
        //para2插入操作
        for (MarketGoodsSpecification tempSpec : goodsInfo.getSpecifications()) {
            tempSpec.setUpdateTime(new Date(System.currentTimeMillis()));
            tempSpec.setGoodsId(goodsid);
            tempSpec.setDeleted(false);
            goodsSpecificationMapper.insert(tempSpec);
        }
        //para3插入操作
        for (MarketGoodsProduct tempProduct : goodsInfo.getProducts()) {
            tempProduct.setUpdateTime(new Date(System.currentTimeMillis()));
            tempProduct.setGoodsId(goodsid);
            tempProduct.setDeleted(false);
            goodsProductMapper.insert(tempProduct);
        }
        //para4插入操作
        for (MarketGoodsAttribute tempAttribute : goodsInfo.getAttributes()) {
            tempAttribute.setUpdateTime(new Date(System.currentTimeMillis()));
            tempAttribute.setGoodsId(goodsid);
            tempAttribute.setDeleted(false);
            goodsAttributeMapper.insert(tempAttribute);
        }


    }


    //catAndBrand 注册cat brand 的mapper
    @Autowired
    MarketCategoryMapper marketCategoryMapper;
    @Autowired
    MarketBrandMapper marketBrandMapper;

    @Override
    public CatAndBrand catAndBrand() {
        //brand List<BrandList>
        List<BrandList> brandLists = marketBrandMapper.selectBrandList();

        //传一个id 作为pid给L2目录索引
        List<CategoryList> categoryLists = marketCategoryMapper.selectCatL1List();
        for (CategoryList tempcat : categoryLists) {
            Integer value = tempcat.getValue();
            tempcat.setChildren(marketCategoryMapper.selectCatL2List(value));
        }
        return new CatAndBrand(categoryLists, brandLists);

        //

    }


}

